package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.dal.mapper.SubPreAuthMapper;
import com.baofu.paycore.dal.model.SubPreAuthDO;
import com.baofu.paycore.manager.storage.Storage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 预授权子单存储
 *
 * <p/>
 *      1、新增订单
 *      2、更新订单
 *      3、废弃订单
 * </p>
 * User: xin deng  Date: 16/8/30 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Repository
public class SubPreAuthManager implements Storage<SubPreAuthDO> {

    /**
     * 预授权资金单底层Mapper
     */
    @Autowired
    private SubPreAuthMapper subPreAuthMapper;

    /**
     * 新增订单
     *
     * @param subPreAuthDO 订单信息
     */
    @Override
    public int addOrder(SubPreAuthDO subPreAuthDO) {

        return subPreAuthMapper.insert(subPreAuthDO);
    }

    /**
     * 更新订单
     *
     * @param subPreAuthDO 订单信息
     */
    @Override
    public int modifyOrder(SubPreAuthDO subPreAuthDO) {
        if (StringUtils.isNotBlank(subPreAuthDO.getErrorReason()) && subPreAuthDO.getErrorReason().length() > 125) {
            subPreAuthDO.setErrorReason(subPreAuthDO.getErrorReason().substring(0, 125));
        }
        return subPreAuthMapper.update(subPreAuthDO);
    }

    /**
     * 废弃订单
     *
     * @param subPreAuthDO 订单信息
     */
    @Override
    public int deleteOrder(SubPreAuthDO subPreAuthDO) {
        return 0;
    }

    /**
     * 依支付单号与资金单类型查询预授权子单信息
     *
     * @param payNo         支付单号
     * @param fundType      资金单类型
     * @return              预授权子单信息
     */
    public SubPreAuthDO selectSubPreAuthByPayNo(String payNo, String fundType){

        return subPreAuthMapper.selectSubPreAuthByPayNo(payNo, fundType);
    }
}
